package com.golxen.framework.common.server.agent.cmd;


import com.golxen.framework.AgentProtocol;
import com.golxen.framework.GameProtocol;
import com.golxen.framework.PBPacket.PBPacket;
import com.golxen.framework.PBPacket.PBPacketUtils;
import com.golxen.framework.common.server.agent.AgentPlayerMgr;
import com.golxen.framework.common.server.game.cmd.Cmd;
import com.golxen.framework.common.server.game.cmd.Command;
import com.golxen.framework.netty.session.agent.ServerClientMgr;
import com.golxen.protojava.LoginProto;
import com.golxen.utils.LogUtil;
import io.netty.channel.ChannelHandlerContext;

@Cmd(code = AgentProtocol.A_LOGIN_REQUEST, desc = "客户端开始登录游戏")
public class AgentLoginGameCmd implements Command {

	@Override
	public Tuple execute(ChannelHandlerContext ctx, PBPacket packet) throws Exception {

		LoginProto.LoginReqMsg reqMsg = LoginProto.LoginReqMsg.parseFrom(packet.toByteArray());
		long userId = reqMsg.getUserId();

		LogUtil.COMMON.info("client login，userId:{} time:{}", userId, System.currentTimeMillis());

		AgentPlayerMgr.addTempChannel(userId, ctx.channel());

		String ip = ctx.channel().remoteAddress().toString();

		LogUtil.COMMON.info("[loginmark] agent login {} {} {} {}", ctx.channel().id().asShortText(),
				ctx.channel().remoteAddress(), userId, ip);

		LoginProto.LoginReqMsg.Builder checkMsg = LoginProto.LoginReqMsg.newBuilder();
		checkMsg.setUserId(userId);
		checkMsg.setIp(ip);
		checkMsg.setClientVer(reqMsg.getClientVer());
		checkMsg.setPhonePlatform(reqMsg.getPhonePlatform());
		checkMsg.setDeviceId(reqMsg.getDeviceId());
		PBPacket message = PBPacketUtils.buildMessage(GameProtocol.S_AGENT_CHECK_LOGIN, userId, checkMsg);
		ServerClientMgr.routeGame(message);

		return new Tuple(null, reqMsg);
	}
}
